package com.opticon.vaadinR.ui.demo;

import com.opticon.vaadinR.interfaces.VaadinRApplication;
import com.opticon.vaadinR.tools.density.HuberExecution;
import com.opticon.vaadinR.ui.field.UploadComponent;
import com.vaadin.data.validator.DoubleValidator;
import com.vaadin.ui.Button;
import com.vaadin.ui.TextField;
import org.apache.log4j.Logger;

import java.io.File;

/**
 * This class is a
 */
public class HuberInputPanel extends AbstractInputPanel {
    private static Logger logger = Logger.getLogger(HuberInputPanel.class);

    private TextField txtFieldPValue;
    private UploadComponent upload;

    public HuberInputPanel(VaadinRApplication aVaadinRApplication) {
        super(aVaadinRApplication);
    }

    @Override
    protected void addInputFields() {
        txtFieldPValue = new TextField("pvalue");
        txtFieldPValue.addValidator(new DoubleValidator("not a valid pvalue"));

        upload = new UploadComponent(iVaadinRApplication);
        addComponent(txtFieldPValue);
        addComponent(upload);
    }

    protected void initExecutionListener() {

        Button.ClickListener lExecutionListener = new Button.ClickListener() {
            public void buttonClick(Button.ClickEvent event) {
                if (!upload.isValid(upload.getValue())) {
                    logger.debug("no valid file provided");
                } else {
                    logger.debug("starting HuberExecution");
                    HuberExecution worker = new HuberExecution(iVaadinRApplication.getRResultHandlerProvider().getRHuberRResultHandler());

//                    DensityExecution worker = new DensityExecution(iVaadinRApplication.getRHuberHandler());

                    File lUploadFile = (File) upload.getValue();

                    worker.addObserver(HuberInputPanel.this);
                    worker.setSessionID(iVaadinRApplication.getSessionID());
                    worker.setFileName(lUploadFile.getAbsolutePath());
                    worker.setPValue(txtFieldPValue.getValue().toString());

                    worker.start();
                    iExecutionProgress.setEnabled(true);
                    iExecutionProgress.setVisible(true);
                    iExecuteButton.setEnabled(false);
                }
            }
        };

        iExecuteButton.addListener(lExecutionListener);
    }
}